package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * Sentinel Nacos Config
 *
 * @author marcher
 */
@Configuration
public class SentinelNacosConfig {

    @Value("${sentinel.nacos.config.serverAddr}")
    private String serverAddr = "localhost:8848";

    @Value("${sentinel.nacos.config.namespace}")
    private String namespace;

    @Bean
    public ConfigService nacosConfigService() throws NacosException {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        properties.put(PropertyKeyConst.NAMESPACE, namespace);

        return ConfigFactory.createConfigService(properties);
    }
}
